<?php
namespace app\admin\controller\account;

use app\admin\BaseController;
use app\admin\model\MoneyCard;
use app\lib\exception\FailedException;
use app\Validate\IdMustPositive;
use app\Validate\MoneyCardAddValidate;
use app\Validate\PageMustValidate;
use think\Request;


class Money extends BaseController
{
    /**
     * 查旬所有会员套餐
     * @param Request $request
     * @return \think\response\Json
     */
    public function money_card(Request $request){
        (new PageMustValidate())->goCheck();
        $post = $request->post();
        $where = [];
        $where['page'] = $post['page'];
        $list = (new MoneyCard())->wechatListTotal($where,'id,price,sort,give_day',"sort desc",$post['page_number']);
        return writeJson(200,$list);
    }
    /**
     * 会员充值添加
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function moneyCardAdd(Request $request){
        (new MoneyCardAddValidate())->goCheck();
        $post = $request->post();
        $new = [
            'price'=>$post['price'],
            'give_day'=>$post['give_day'],
            'sort'=>$post['sort'],
        ];
        $res = (new MoneyCard())->dataUpdate($new);
        if(!$res){
            throw new FailedException(['msg'=>'会员充值添加失败']);
        }
        return writeJson(200,[],'会员充值添加成功');

    }
    /**
     * 会员充值编辑
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function moneyCardEdit(Request $request){
        (new IdMustPositive())->goCheck();
        (new MoneyCardAddValidate())->goCheck();
        $post = $request->post();
        $new = [
            'id'   =>$post['id'],
            'price'=>$post['price'],
            'give_day'=>$post['give_day'],
            'sort'=>$post['sort'],
        ];
        $res = (new MoneyCard())->dataUpdate($new);
        if(!$res){
            throw new FailedException(['msg'=>'会员充值编辑失败']);
        }
        return writeJson(200,[],'会员充值编辑成功');
    }
    /**
     * 会员充值的删除
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function moneyCardDelete(Request $request){
        $post = $request->post();
        if(count($post['id'])==0){
            throw new FailedException(['msg'=>'请选择需要删除的资讯']);
        }
        $res = (new MoneyCard())->fieldUpdate('delete_time',time(),"",[['id','in',$post['id']]]);
        if(!$res){
            throw new FailedException(['msg'=>'会员充值删除失败']);
        }
        return writeJson(200,[],'会员充值删除成功');
    }

}
